package com.wkck.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.wkck.domain.News;
import com.wkck.service.NewsService;
import com.wkck.utils.Code;
import com.wkck.utils.Msg;
import com.wkck.utils.Result;
import lombok.extern.slf4j.Slf4j;
import org.apache.logging.log4j.util.Strings;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import java.util.List;

@Slf4j
@RestController
@RequestMapping("/news")
public class NewsController {

    @Resource
    private NewsService newsService;

    /**
     * 得到发布时间最近的四篇新闻详细信息
     * @return
     * ignore
     */

//    @GetMapping("/list")
//    @Cacheable(value = "newsList")
//    public Result getList(){
//        return new Result(Code.SELECT_OK,newsService.newsList(), Msg.SELECT_OK);
//    }

    /**
     * 根据id得到该新闻详情
     * @param id
     * @return
     * ignore
     */

//    @GetMapping("/{id}")
//    @Cacheable(value = "newsById",key = "#id")
//    public Result getByIdNews(@PathVariable Integer id){
//        return new Result(Code.SELECT_OK,newsService.getById(id),Msg.SELECT_OK);
//    }

    /**
     * 得到最近10条的新闻
     * @param currentPage
     * @param pageSize
     * @return
     * ignore
     */

//    @GetMapping("/{currentPage}/{pageSize}")
//    @Cacheable(value = "NewsListPage")
//    public Result getNewsListPage(@PathVariable Integer currentPage,@PathVariable Integer pageSize){
//        IPage<News> page = new Page<>(currentPage,pageSize);
//        LambdaQueryWrapper<News> lqw = new LambdaQueryWrapper<>();
//        lqw.orderByDesc(News::getNews_time);
//        return new Result(Code.SELECT_OK,newsService.page(page,lqw),Msg.SELECT_OK);
//    }

    /**
     * 根据参数查询新闻
     * @param currentPage
     * @param pageSize
     * @param news
     * @return
     */

//    @GetMapping("/have/{currentPage}/{pageSize}")
//    @Cacheable(value = "NewsListPageAndParams")
//    public Result getNewsListPageAndParams(@PathVariable Integer currentPage,@PathVariable Integer pageSize,News news){
//        IPage<News> page = new Page<>(currentPage,pageSize);
//        LambdaQueryWrapper<News> lqw = new LambdaQueryWrapper<>();
//        lqw.like(Strings.isNotEmpty(news.getNews_name()),News::getNews_name,news.getNews_name())
//                .like(Strings.isNotEmpty(news.getNews_issuer()),News::getNews_issuer,news.getNews_issuer())
//                .orderByDesc(News::getNews_time);
//        return new Result(Code.SELECT_OK,newsService.page(page,lqw),Msg.SELECT_OK);
//    }

    /**
     * 后台首页的新闻列表
     * @return
     * ignore
     */

//    @GetMapping("/10")
//    @Cacheable(value = "getNewsListLimit10")
//    public Result getNewsListLimit10(){
//
//        LambdaQueryWrapper<News> queryWrapper = new LambdaQueryWrapper<>();
//        queryWrapper
//                .orderByDesc(News::getNews_time)
//                .last("limit 8");
//        List<News> newsList = newsService.list(queryWrapper);
//        log.info("后台新闻列表数据:{}",newsList);
//        return new Result(Code.SELECT_OK,newsList,Msg.SELECT_OK);
//    }
}
